package com.xiaoming.service.impl;

import com.xiaoming.entity.HealthRecord;
import com.xiaoming.entity.HealthTask;
import com.xiaoming.entity.User;
import com.xiaoming.mapper.HealthRecordMapper;
import com.xiaoming.mapper.HealthTaskMapper;
import com.xiaoming.mapper.UserMapper;
import com.xiaoming.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jxbai
 * @since 2020-07-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private HealthRecordMapper healthRecordMapper;

    @Autowired
    private HealthTaskMapper healthTaskMapper;

    @Override
    public void processHealthRecords() {
        insertHealthRecords();
    }

    private List<Integer> insertHealthRecords() {
        List<Integer> result = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            HealthRecord healthRecord = insertHealthRecord(i+1);
            insertHealthTask(i+1, healthRecord);
            result.add(healthRecord.getRecordId().intValue());
        }
        return result;
    }

    private void insertHealthTask(int i, HealthRecord healthRecord) {
        HealthTask healthTask = new HealthTask();
        healthTask.setRecordId(healthRecord.getRecordId());
        healthTask.setUserId(i);
        healthTask.setTaskName("TaskName" + i);
        healthTaskMapper.insert(healthTask);
    }

    private HealthRecord insertHealthRecord(int i) {
        HealthRecord healthRecord = new HealthRecord();
        healthRecord.setUserId(i);
        healthRecord.setLevelId(i % 5);
        healthRecord.setRemark("Remark" + i);
        healthRecordMapper.insert(healthRecord);
        return healthRecord;
    }
}
